package com.hlz.rabbit.mq.topic;

import com.hlz.rabbit.mq.util.RabbitMqUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**

 *(星号)可以代替一个单词
 #(井号)可以替代零个或多个单词

 * 当一个队列绑定键是#,那么这个队列将接收所有数据，就有点像 fanout 了
 * 如果队列绑定键当中没有#和*出现，那么该队列绑定类型就是 direct 了
 * @author Hongliang Zhu
 * @create 2022-11-21 20:56
 */
public class TopicProducer {

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMqUtils.getChannel();

        // 定义交换机
        channel.exchangeDeclare("my-topic", BuiltinExchangeType.TOPIC, false);

        // 定义队列
        channel.queueDeclare("test-queue1", false, false, false, null);
        channel.queueDeclare("test-queue2", false, false, false, null);
        channel.queueDeclare("test-queue3", false, false, false, null);

        // 队列绑定
        channel.queueBind("test-queue1", "my-topic", "#.order.error");
        channel.queueBind("test-queue2", "my-topic", "#.error");
        channel.queueBind("test-queue3", "my-topic", "*.info.*");

        // 发送消息
        channel.basicPublish("my-topic", "test.order.error", null, "test.order.error == >order订单日志，级别为error".getBytes());
        channel.basicPublish("my-topic", "hi.test.order.error", null, "hi.test.order.error == > order订单日志，级别为error".getBytes());
        channel.basicPublish("my-topic", "test.info.apple", null, "test.info.apple == >info日志".getBytes());
        channel.basicPublish("my-topic", "test.error", null, "test.info.apple == >info日志".getBytes());

        RabbitMqUtils.console(channel, "my-topic", "test.order.error");

    }
}
